local windowList = findWindowList("*","TWNClientFramework");
if( #windowList == 0 ) then
	print("You need to run Rift first!");
	return 0;
end

function getWin()
	if( __WIN == nil ) then
  		__WIN = windowList[1]
	end

	return __WIN;
end	

function getProc()
	if( __PROC == nil or not windowValid(__WIN) ) then
		if( __PROC ) then closeProcess(__PROC) end;
		__PROC = openProcess( findProcessByWindow(getWin()) );
	end

	return __PROC;
end

function memoryReadRepeat(_type, proc, address, offset)
	local readfunc;
	local ptr = false;
	local val;

	if( type(proc) ~= "userdata" ) then
		error("Invalid proc", 2);
	end

	if( type(address) ~= "number" ) then
		error("Invalid address", 2);
	end

	if( _type == "int" ) then
		readfunc = memoryReadInt;
	elseif( _type == "uint" ) then
		readfunc = memoryReadUInt;
	elseif( _type == "float" ) then
		readfunc = memoryReadFloat;
	elseif( _type == "byte" ) then
		readfunc = memoryReadByte;
	elseif( _type == "string" ) then
		readfunc = memoryReadString;
	elseif( _type == "ustring" ) then
		readfunc = memoryReadUString;
	elseif( _type == "intptr" ) then
		readfunc = memoryReadIntPtr;
		ptr = true;
	elseif( _type == "uintptr" ) then
		readfunc = memoryReadUIntPtr;
		ptr = true;
	elseif( _type == "byteptr" ) then
		readfunc = memoryReadBytePtr;
		ptr = true;
	elseif( _type == "floatptr" ) then
		readfunc = memoryReadFloatPtr;
		ptr = true;		
	else
		return nil;
	end

	for i = 1, 10 do
		showWarnings(false);
		if( ptr ) then
			val = readfunc(proc, address, offset);
		else
			val = readfunc(proc, address);
		end
		showWarnings(true);

		if( val ) then i = 11; end; -- Get out of loop
	end

	if( val == nil ) then
		local info = debug.getinfo(2);
		local name;
		if( info.name ) then
			name = info.name .. '()';
		else
			name = '<unknown>';
		end
--		logger:log('debug',  "Error reading memory in %s from %s:%d", name, info.short_src, info.currentline or 0);
--	no logger available at the time of using that
		local msg = sprintf("Error reading memory in %s from %s:%d", name, info.short_src, info.currentline or 0);
		error(msg, 2)

	end

	return val

end	
local mywin, proc, mainOffset
repeat 
   mywin = findProcess("RIFT*");
   proc = openProcess(mywin);
   mainOffset = getModuleAddress(mywin, "rift.exe");
until mainOffset ~= 0
printf("main offset 0x%X\n",mainOffset)


_Address = include("addresses.lua")
_Charbase = memoryReadUIntPtr(getProc(),mainOffset+_Address.Charbase,_Address.Charbaseoffsets)


function Havetarget()
	return memoryReadInt(getProc(),mainOffset+_Address.Havetarget) == 1 
end

function Incombat()
	return memoryReadInt(getProc(),mainOffset+_Address.Incombat) == 1
end
local Skillcount = 0

function Useskills()
	keyboardPress(key.VK_3) -- global cooldown skill
	yrest(1000)
	Skillcount = Skillcount + 1
	if Skillcount == 5 then
		keyboardPress(key.VK_4) -- combo point skill
		yrest(1000)
		Skillcount = 0
	end
end

attach(getWin())
setStartKey(key.VK_F5);
setStopKey(key.VK_F6);
local hf_x, hf_y, hf_wide, hf_high = windowRect(getWin())
printf("%d %d %d %d\n", hf_wide, hf_high, hf_x, hf_y );
print("ok starting")
print(os.date())
local tracktime = os.time()
function main()
	while (true) do
		if os.difftime(os.time(), tracktime) >= 10 then
			keyboardPress(key.VK_1)
			yrest(1000)
			keyboardPress(key.VK_ESCAPE)
			print("following")
			tracktime = os.time()
		end
		keyboardPress(key.VK_2)
		yrest(500)
		if Havetarget() then
			Useskills()
		end
		yrest(500)
	end
end
startMacro(main,true);
-- key 1, follow
-- key 2, assist
-- key 3, attack
-- key 4, rogue attack

--memoryReadIntPtr(getProc(),mainOffset+_Address.Fishbase,foffset)
--attach(getWin())
--keyboardHold(key.VK_A)
--keyboardRelease(key.VK_A)